Skip to content

CrossFadeWidget

Base classes

Name Children Inherits
Widget
prettyqt.widgets.widget
The base class of all user interface objects.

â‹” Inheritance diagram

graph TD
  370107565["custom_widgets.CrossFadeWidget"]
  3664736470["widgets.Widget"]
  829357528["widgets.WidgetMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  625385617["gui.PaintDeviceMixin"]
  3620195106["QtWidgets.QWidget"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  673956333["QtGui.QPaintDevice"]
  3664736470 --> 370107565
  829357528 --> 3664736470
  3227531497 --> 829357528
  2751680898 --> 3227531497
  625385617 --> 829357528
  2751680898 --> 625385617
  2751680898 --> 829357528
  3620195106 --> 3664736470
  3984060476 --> 3620195106
  37984305 --> 3984060476
  2751680898 --> 37984305
  673956333 --> 3620195106
  37984305 --> 673956333

🛈 DocStrings

Bases: Widget

Source code in prettyqt/custom_widgets/crossfadewidget.py
class CrossFadeWidget(widgets.Widget):
    def __init__(
        self,
        pixmap_1: gui.QPixmap | os.PathLike[str] | str | widgets.QWidget | None = None,
        pixmap_2: gui.QPixmap | os.PathLike[str] | str | widgets.QWidget | None = None,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.pixmap_1 = None
        self.pixmap_2 = None
        self.set_pixmap_1(pixmap_1)
        self.set_pixmap_2(pixmap_2)
        self.set_blend_factor(0.0)
        self.timeline = core.TimeLine(finished=self.close)
        self.timeline.valueChanged.connect(self.set_blend_factor)
        self.set_size_policy("fixed", "fixed")

    def start_fade(self, duration: int = 500):
        self.timeline.setDuration(duration)
        self.timeline.start()
        # self.show()

    def set_pixmap_1(
        self, pixmap: gui.QPixmap | os.PathLike[str] | str | widgets.QWidget | None
    ):
        self.pixmap_1 = self._get_pixmap(pixmap)
        self.updateGeometry()
        self.repaint()

    def set_pixmap_2(
        self, pixmap: gui.QPixmap | os.PathLike[str] | str | widgets.QWidget | None
    ):
        self.pixmap_2 = self._get_pixmap(pixmap)
        self.updateGeometry()
        self.repaint()

    @staticmethod
    def _get_pixmap(
        pixmap: gui.QPixmap | os.PathLike[str] | str | widgets.QWidget | None,
    ):
        match pixmap:
            case gui.QPixmap():
                return pixmap
            case os.PathLike() | str():
                return gui.Pixmap(os.fspath(pixmap))
            case widgets.QWidget():
                return pixmap.grab()
            case None:
                return gui.Pixmap()

    def set_blend_factor(self, factor: float):
        self._blend_factor = factor
        self.repaint()

    def get_blend_factor(self) -> float:
        """Pixmap blending factor between 0.0 and 1.0."""
        return self._blend_factor

    def sizeHint(self):
        """Return max size of both."""
        size1 = self.pixmap_1.size() if self.pixmap_1 else super().sizeHint()
        size2 = self.pixmap_2.size() if self.pixmap_2 else super().sizeHint()
        return size1.expandedTo(size2)

    def paintEvent(self, event):
        """Paint the interpolated pixmap image."""
        with gui.Painter(self) as p:
            p.setClipRect(event.rect())
            factor = self.get_blend_factor() ** 2
            if self.pixmap_1 and 1.0 - factor:
                p.setOpacity(1.0 - factor)
                p.drawPixmap(core.Point(0, 0), self.pixmap_1)
            if self.pixmap_2 and factor:
                p.setOpacity(factor)
                p.drawPixmap(core.Point(0, 0), self.pixmap_2)

    blend_factor = core.Property(
        float,
        get_blend_factor,
        set_blend_factor,
        doc="Blend factor between the two pixmaps.",
    )

get_blend_factor() -> float

Pixmap blending factor between 0.0 and 1.0.

Source code in prettyqt/custom_widgets/crossfadewidget.py
def get_blend_factor(self) -> float:
    """Pixmap blending factor between 0.0 and 1.0."""
    return self._blend_factor

paintEvent(event)

Paint the interpolated pixmap image.

Source code in prettyqt/custom_widgets/crossfadewidget.py
def paintEvent(self, event):
    """Paint the interpolated pixmap image."""
    with gui.Painter(self) as p:
        p.setClipRect(event.rect())
        factor = self.get_blend_factor() ** 2
        if self.pixmap_1 and 1.0 - factor:
            p.setOpacity(1.0 - factor)
            p.drawPixmap(core.Point(0, 0), self.pixmap_1)
        if self.pixmap_2 and factor:
            p.setOpacity(factor)
            p.drawPixmap(core.Point(0, 0), self.pixmap_2)

sizeHint()

Return max size of both.

Source code in prettyqt/custom_widgets/crossfadewidget.py
def sizeHint(self):
    """Return max size of both."""
    size1 = self.pixmap_1.size() if self.pixmap_1 else super().sizeHint()
    size2 = self.pixmap_2.size() if self.pixmap_2 else super().sizeHint()
    return size1.expandedTo(size2)

:fa-brands-github: Show source on GitHub